package com.campus.client.view;

import com.campus.client.controller.LoginController;
import com.campus.client.net.ClientConnector;
import com.campus.common.vo.User;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class LoginFrame extends Application {

    private ToggleGroup identityGroup;
    private TextField usernameField;
    private PasswordField passwordField; // 改为PasswordField
    private Button loginButton;
    private Button registerButton;
    private RadioButton studentRadio;
    private RadioButton teacherRadio;
    private RadioButton adminRadio;

    // 在LoginFrame类中添加
    private LoginController controller;

    // 在start方法中创建控制器实例
    @Override
    public void start(Stage primaryStage) throws IOException {
        // 创建主容器
        VBox root = createLoginInterface();

        // 创建控制器
        ClientConnector connector = new ClientConnector("172.20.10.2", 8888);
        controller = new LoginController(this, connector);


        // 创建场景并设置到舞台
        Scene scene = new Scene(root, 640, 400);
        primaryStage.setTitle("用户登录");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private VBox createLoginInterface() {
        // 创建主VBox
        VBox mainVBox = new VBox();
        mainVBox.setPrefSize(640, 400);

        // 创建AnchorPane作为主容器
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        VBox.setVgrow(anchorPane, Priority.ALWAYS);

        // 创建背景图片
        ImageView backgroundImage = new ImageView();
        try {
            Image bgImage = new Image("login.png");
            backgroundImage.setImage(bgImage);
        } catch (Exception e) {
            System.out.println("无法加载背景图片: " + e.getMessage());
        }
        backgroundImage.setFitHeight(400.0);
        backgroundImage.setFitWidth(640.0);
        backgroundImage.setPreserveRatio(true);
        backgroundImage.setPickOnBounds(true);

        // 创建背景矩形
        Rectangle backgroundRect = new Rectangle();
        backgroundRect.setOpacity(0.51);
        backgroundRect.setLayoutX(81);
        backgroundRect.setLayoutY(107);
        backgroundRect.setWidth(471);
        backgroundRect.setHeight(186);
        backgroundRect.setArcWidth(5);
        backgroundRect.setArcHeight(5);
        backgroundRect.setFill(javafx.scene.paint.Color.web("#fbfbfb"));
        backgroundRect.setStroke(javafx.scene.paint.Color.BLACK);
        backgroundRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);

        // 创建标题标签
        Text titleText = new Text("用户登录");
        titleText.setFill(Color.WHITE);
        titleText.setLayoutX(247);
        titleText.setLayoutY(80);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 40));

        // 创建用户名和密码区域
        HBox credentialsBox = createCredentialsBox();
        credentialsBox.setLayoutX(119);
        credentialsBox.setLayoutY(131);

        // 创建按钮区域
        HBox buttonBox = createButtonBox();
        buttonBox.setLayoutX(144);
        buttonBox.setLayoutY(314);

        // 创建身份选择区域
        HBox identityBox = createIdentityBox();
        identityBox.setLayoutX(145);
        identityBox.setLayoutY(231);

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(
                backgroundImage, backgroundRect, titleText, credentialsBox, buttonBox, identityBox
        );

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);

        return mainVBox;
    }

    private HBox createCredentialsBox() {
        HBox credentialsBox = new HBox();
        credentialsBox.setPrefSize(395, 91);

        // 创建标签VBox
        VBox labelBox = new VBox(10);
        labelBox.setAlignment(javafx.geometry.Pos.CENTER);
        labelBox.setPrefSize(100, 103);

        Label usernameLabel = new Label("用户名：");
        usernameLabel.setFont(Font.font(25));

        Label passwordLabel = new Label("密码：");
        passwordLabel.setFont(Font.font(25));

        labelBox.getChildren().addAll(usernameLabel, passwordLabel);

        // 创建输入框VBox
        VBox inputBox = new VBox(10);
        inputBox.setPrefSize(285, 183);

        usernameField = new TextField();
        usernameField.setPrefSize(237, 41);

        // 使用PasswordField而不是TextField
        passwordField = new PasswordField();
        passwordField.setPrefSize(237, 41);

        inputBox.getChildren().addAll(usernameField, passwordField);

        // 将标签和输入框添加到HBox
        credentialsBox.getChildren().addAll(labelBox, inputBox);

        return credentialsBox;
    }

    private HBox createButtonBox() {
        HBox buttonBox = new HBox(50);
        buttonBox.setAlignment(javafx.geometry.Pos.CENTER);
        buttonBox.setPrefSize(345, 45);

        // 创建登录按钮
        loginButton = new Button("登录");
        loginButton.setMnemonicParsing(false);
        loginButton.setPrefSize(128, 47);
        loginButton.setStyle("-fx-background-color: #35c9ff;");
        loginButton.setTextFill(javafx.scene.paint.Color.WHITE);
        loginButton.setFont(Font.font("System", FontWeight.BOLD, FontPosture.ITALIC, 20));

        // 创建注册按钮
        registerButton = new Button("注册");
        registerButton.setMnemonicParsing(false);
        registerButton.setPrefSize(128, 47);
        registerButton.setStyle("-fx-background-color: grey;");
        registerButton.setTextFill(javafx.scene.paint.Color.WHITE);
        registerButton.setFont(Font.font("System", FontWeight.BOLD, 20));

        // 添加按钮到HBox
        buttonBox.getChildren().addAll(loginButton, registerButton);

        return buttonBox;
    }

    private HBox createIdentityBox() {
        HBox identityBox = new HBox(30);
        identityBox.setAlignment(javafx.geometry.Pos.CENTER);
        identityBox.setPrefSize(372, 68);

        // 创建单选按钮组
        identityGroup = new ToggleGroup();

        // 创建学生单选按钮
        studentRadio = new RadioButton("学生");
        studentRadio.setMnemonicParsing(false);
        studentRadio.setFont(Font.font(20));
        studentRadio.setToggleGroup(identityGroup);
        studentRadio.setSelected(true); // 默认选中学生

        // 创建教师单选按钮
        teacherRadio = new RadioButton("教师");
        teacherRadio.setMnemonicParsing(false);
        teacherRadio.setFont(Font.font(20));
        teacherRadio.setToggleGroup(identityGroup);

        // 创建管理员单选按钮
        adminRadio = new RadioButton("管理员");
        adminRadio.setMnemonicParsing(false);
        adminRadio.setFont(Font.font(20));
        adminRadio.setToggleGroup(identityGroup);

        // 添加单选按钮到HBox
        identityBox.getChildren().addAll(studentRadio, teacherRadio, adminRadio);

        return identityBox;
    }

    // 添加事件处理方法
    public void setLoginButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        loginButton.setOnAction(handler::accept);
    }

    public void setRegisterButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        registerButton.setOnAction(handler::accept);
    }

    // 获取用户输入的方法
    public String getUsername() {
        return usernameField.getText();
    }

    public String getPassword() {
        return passwordField.getText(); // PasswordField的getText()方法同样返回文本内容
    }

    public String getSelectedIdentity() {
        RadioButton selected = (RadioButton) identityGroup.getSelectedToggle();
        return selected != null ? selected.getText() : "";
    }

    public void clearPasswordField() {
        this.passwordField.clear();
    }

    public void showMainFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage loginStage = (Stage) loginButton.getScene().getWindow();

        // 创建主界面
        StudentMainFrame mainFrame = new StudentMainFrame();
        mainFrame.setUser(getUsername(),getPassword(),getSelectedIdentity());
        Stage mainStage = new Stage();

        // 显示主界面
        mainFrame.show(mainStage);

        // 关闭登录窗口
        loginStage.close();
    }

    public void showTeacherMainFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage loginStage = (Stage) loginButton.getScene().getWindow();

        // 创建主界面
        TeacherMainFrame teacherMainFrame = new TeacherMainFrame();
        teacherMainFrame.setUser(getUsername(),getPassword(),getSelectedIdentity());
        Stage mainStage = new Stage();

        // 显示主界面
        teacherMainFrame.show(mainStage);

        // 关闭登录窗口
        loginStage.close();
    }

    public void showAdminMainFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage loginStage = (Stage) loginButton.getScene().getWindow();

        // 创建主界面
        AdminMainFrame adminMainFrame = new AdminMainFrame();
        adminMainFrame.setUser(getUsername(),getPassword(),getSelectedIdentity());
        Stage mainStage = new Stage();

        // 显示主界面
        adminMainFrame.show(mainStage);

        // 关闭登录窗口
        loginStage.close();
    }

    public void showStudentLoginFrame() throws IOException {
        Stage loginStage = (Stage) loginButton.getScene().getWindow();
        StudentRegisterFrame studentRegisterFrame = new StudentRegisterFrame();
        Stage mainStage = new Stage();

        studentRegisterFrame.show(mainStage,usernameField.getText(),passwordField.getText());
        loginStage.close();
    }

    public void showTeacherLoginFrame() throws IOException {
        Stage loginStage = (Stage) loginButton.getScene().getWindow();
        TeacherRegisterFrame teacherRegisterFrame = new TeacherRegisterFrame();
        Stage mainStage = new Stage();

        teacherRegisterFrame.show(mainStage,usernameField.getText(),passwordField.getText());
        loginStage.close();
    }
}
